package com.itheima.vms.controller;


import com.itheima.vms.domain.PageInfo;
import com.itheima.vms.domain.PageResult;
import com.itheima.vms.domain.po.SysUser;
import com.itheima.vms.response.Result;
import com.itheima.vms.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;


    //获取用户数据
    @GetMapping
    public Result<List<SysUser>> list(){

        return Result.ok(sysUserService.list());
    }

    //分页查询
    //条件分页查询
    @GetMapping("/page")
    public Result queryUser(PageInfo<SysUser> pageInfo ){
        PageResult pageResult = sysUserService.queryUserByPage(pageInfo);
        return Result.ok(pageResult);
    }


    //添加用户
    @PostMapping
    public Result add(@RequestBody SysUser sysUser) throws Exception {
        sysUserService.add(sysUser);
        return Result.ok();
    }
    //删除用户
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.ok();
    }

    //回显查询
    @GetMapping("/{id}")
    public Result<SysUser> getUserById(@PathVariable Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    //修改用户
    @PutMapping
    public Result update(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.ok();
    }


}